| Conditions | 1 |
| Paths | 512 |
| Total Lines | 239 |
| Lines | 163 |
| Ratio | 68.2 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 23 | $.fn.symphonyDrawer = function(options) { |
||
| 24 | var objects = this, |
||
| 25 | wrapper = $('#wrapper'), |
||
| 26 | contents = $('#contents'), |
||
| 27 | form = contents.find('> form'), |
||
| 28 | settings = { |
||
| 29 | verticalWidth: 300, |
||
| 30 | speed: 'fast' |
||
| 31 | }; |
||
| 32 | |||
| 33 | $.extend(settings, options); |
||
| 34 | |||
| 35 | /*------------------------------------------------------------------------- |
||
| 36 | Events |
||
| 37 | -------------------------------------------------------------------------*/ |
||
| 38 | |||
| 39 | // Expand drawer |
||
| 40 | objects.on('expand.drawer', function expand(event, speed, stay) { |
||
| 41 | View Code Duplication | var drawer = $(this), |
|
|
|
|||
| 42 | position = drawer.data('position'), |
||
| 43 | buttons = $('.button.drawer'), |
||
| 44 | button = buttons.filter('[href="#' + drawer.attr('id') + '"]'), |
||
| 45 | samePositionButtons = buttons.filter('.' + position), |
||
| 46 | context = drawer.data('context') ? '.' + drawer.data('context') : '', |
||
| 47 | height = getHeight(); |
||
| 48 | |||
| 49 | drawer.trigger('expandstart.drawer'); |
||
| 50 | |||
| 51 | speed = (typeof speed === 'undefined' ? settings.speed : speed); |
||
| 52 | stay = (typeof stay === 'undefined' ? false : true); |
||
| 53 | |||
| 54 | // update button state |
||
| 55 | samePositionButtons.removeClass('selected'); |
||
| 56 | |||
| 57 | // Close opened drawers from same region |
||
| 58 | $('.drawer.' + position).filter(function(index) { |
||
| 59 | return $(this).data('open'); |
||
| 60 | }).trigger('collapse.drawer', [speed, true]); |
||
| 61 | |||
| 62 | if (position === 'vertical-left') { |
||
| 63 | drawer.css({ |
||
| 64 | width: 0, |
||
| 65 | height: height, |
||
| 66 | display: 'block' |
||
| 67 | }) |
||
| 68 | .animate({ |
||
| 69 | width: settings.verticalWidth |
||
| 70 | }, { |
||
| 71 | duration: speed, |
||
| 72 | step: function(now, fx){ |
||
| 73 | form.css('margin-left', now + 1); // +1px right border |
||
| 74 | }, |
||
| 75 | complete: function() { |
||
| 76 | form.css('margin-left', settings.verticalWidth + 1); // +1px right border |
||
| 77 | drawer.trigger('expandstop.drawer'); |
||
| 78 | } |
||
| 79 | }); |
||
| 80 | } |
||
| 81 | else if (position === 'vertical-right') { |
||
| 82 | drawer.css({ |
||
| 83 | width: 0, |
||
| 84 | height: height, |
||
| 85 | display: 'block' |
||
| 86 | }) |
||
| 87 | .animate({ |
||
| 88 | width: settings.verticalWidth |
||
| 89 | }, { |
||
| 90 | duration: speed, |
||
| 91 | step: function(now, fx){ |
||
| 92 | form.css('margin-right', now + 1); // +1px left border |
||
| 93 | }, |
||
| 94 | complete: function() { |
||
| 95 | form.css('margin-right', settings.verticalWidth + 1); // +1px right border |
||
| 96 | drawer.trigger('expandstop.drawer'); |
||
| 97 | } |
||
| 98 | }); |
||
| 99 | } |
||
| 100 | else if (position === 'horizontal') { |
||
| 101 | drawer.animate({ |
||
| 102 | height: 'show' |
||
| 103 | }, { |
||
| 104 | duration: speed, |
||
| 105 | complete: function() { |
||
| 106 | drawer.trigger('expandstop.drawer'); |
||
| 107 | } |
||
| 108 | }); |
||
| 109 | } |
||
| 110 | |||
| 111 | button.addClass('selected'); |
||
| 112 | |||
| 113 | // store state |
||
| 114 | if(Symphony.Support.localStorage === true) { |
||
| 115 | // Put in a try/catch incase we exceed storage space |
||
| 116 | try { |
||
| 117 | window.localStorage['symphony.drawer.' + drawer.attr('id') + context] = 'opened'; |
||
| 118 | } |
||
| 119 | catch(e) { |
||
| 120 | window.onerror(e.message); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | wrapper.addClass('drawer-' + position); |
||
| 125 | drawer.data('open', true); |
||
| 126 | }); |
||
| 127 | |||
| 128 | // Collapse drawer |
||
| 129 | objects.on('collapse.drawer', function collapse(event, speed, stay) { |
||
| 130 | View Code Duplication | var drawer = $(this), |
|
| 131 | position = drawer.data('position'), |
||
| 132 | buttons = $('.button.drawer'), |
||
| 133 | button = buttons.filter('[href="#' + drawer.attr('id') + '"]'), |
||
| 134 | context = drawer.data('context') ? '.' + drawer.data('context') : ''; |
||
| 135 | |||
| 136 | drawer.trigger('collapsestart.drawer'); |
||
| 137 | |||
| 138 | speed = (typeof speed === 'undefined' ? settings.speed : speed); |
||
| 139 | stay = (typeof stay === 'undefined' ? false : true); |
||
| 140 | |||
| 141 | // update button state |
||
| 142 | button.removeClass('selected'); |
||
| 143 | |||
| 144 | if (position === 'vertical-left') { |
||
| 145 | drawer.animate({ |
||
| 146 | width: 0 |
||
| 147 | }, { |
||
| 148 | duration: speed, |
||
| 149 | step: function(now, fx){ |
||
| 150 | if (!stay) { |
||
| 151 | form.css('margin-left', now); |
||
| 152 | } |
||
| 153 | }, |
||
| 154 | complete: function() { |
||
| 155 | drawer.css({ |
||
| 156 | display: 'none' |
||
| 157 | }) |
||
| 158 | .trigger('collapsestop.drawer'); |
||
| 159 | } |
||
| 160 | }); |
||
| 161 | } |
||
| 162 | else if (position === 'vertical-right') { |
||
| 163 | drawer.animate({ |
||
| 164 | width: 0 |
||
| 165 | }, { |
||
| 166 | duration: speed, |
||
| 167 | step: function(now, fx){ |
||
| 168 | if (!stay) { |
||
| 169 | form.css('margin-right', now); |
||
| 170 | } |
||
| 171 | }, |
||
| 172 | complete: function() { |
||
| 173 | drawer.css({ |
||
| 174 | display: 'none' |
||
| 175 | }) |
||
| 176 | .trigger('collapsestop.drawer'); |
||
| 177 | } |
||
| 178 | }); |
||
| 179 | } |
||
| 180 | else if (position === 'horizontal') { |
||
| 181 | drawer.animate({ |
||
| 182 | height: 'hide' |
||
| 183 | }, { |
||
| 184 | duration: speed, |
||
| 185 | complete: function() { |
||
| 186 | drawer.trigger('collapsestop.drawer'); |
||
| 187 | } |
||
| 188 | }); |
||
| 189 | } |
||
| 190 | |||
| 191 | // store state |
||
| 192 | if(Symphony.Support.localStorage === true) { |
||
| 193 | // Put in a try/catch incase we exceed storage space |
||
| 194 | try { |
||
| 195 | window.localStorage['symphony.drawer.' + drawer.attr('id') + context] = 'closed'; |
||
| 196 | } |
||
| 197 | catch(e) { |
||
| 198 | window.onerror(e.message); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | wrapper.removeClass('drawer-' + position); |
||
| 203 | drawer.data('open', false); |
||
| 204 | }); |
||
| 205 | |||
| 206 | // Resize drawers |
||
| 207 | $(window).on('resize.drawer load.drawer', function() { |
||
| 208 | var height = getHeight(); |
||
| 209 | objects.filter('.vertical-left, .vertical-right').css('height', height); |
||
| 210 | }); |
||
| 211 | |||
| 212 | /*------------------------------------------------------------------------- |
||
| 213 | Utilities |
||
| 214 | -------------------------------------------------------------------------*/ |
||
| 215 | |||
| 216 | var getHeight = function() { |
||
| 217 | var height = Math.max(window.innerHeight - contents[0].offsetTop - 1, contents[0].clientHeight); |
||
| 218 | |||
| 219 | return height; |
||
| 220 | }; |
||
| 221 | |||
| 222 | /*------------------------------------------------------------------------- |
||
| 223 | Initialisation |
||
| 224 | -------------------------------------------------------------------------*/ |
||
| 225 | |||
| 226 | objects.each(function drawers() { |
||
| 227 | var drawer = $(this), |
||
| 228 | position = drawer.data('position'), |
||
| 229 | button = $('.button.drawer[href="#' + drawer.attr('id') + '"]'), |
||
| 230 | context = drawer.data('context') ? '.' + drawer.data('context') : '', |
||
| 231 | storedState; |
||
| 232 | |||
| 233 | // Initial state |
||
| 234 | if (drawer.data('default-state') === 'opened') { |
||
| 235 | drawer.data('open', true); |
||
| 236 | } |
||
| 237 | // Restore state |
||
| 238 | if (Symphony.Support.localStorage === true) { |
||
| 239 | storedState = window.localStorage['symphony.drawer.' + drawer.attr('id') + context]; |
||
| 240 | if (storedState === 'opened') { |
||
| 241 | drawer.data('open', true); |
||
| 242 | } |
||
| 243 | else if (storedState === 'closed') { |
||
| 244 | drawer.data('open', false); |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | // Click event for the related button |
||
| 249 | button.on('click.drawer', function(event) { |
||
| 250 | event.preventDefault(); |
||
| 251 | !drawer.data('open') ? drawer.trigger('expand.drawer') : drawer.trigger('collapse.drawer'); |
||
| 252 | }); |
||
| 253 | |||
| 254 | // Initially opened drawers |
||
| 255 | drawer.data('open') ? drawer.trigger('expand.drawer', [0]) : drawer.trigger('collapse.drawer', [0, true]); |
||
| 256 | }); |
||
| 257 | |||
| 258 | /*-----------------------------------------------------------------------*/ |
||
| 259 | |||
| 260 | return objects; |
||
| 261 | }; |
||
| 262 | |||
| 264 |